#include <stdio.h>
#include <time.h>
#include "unit_test.h"
typedef unsigned char uint8;
typedef unsigned short int uint16;
    static uint8 * the_months[] =
    {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    } ;
//--------------------------------------------------------------------------------------------------
//  Function Name: print_hex_dump()
//
//    Description: We're given a pointer to data to display and a
//                 number of bytes we're to display, then we dump
//                 the data to the serial port in ASCII HEX.  We
//                 also attempt to display the ASCII hex data of
//                 the characters.
//
// Pre-Conditions:
//
//     Parameters
//          Local: from_here_p - A pointer to the start of the data
//                 to dump to ASCII HEX.
//
//                 byte_count - The number of bytes to dump.
//
//        Globals:
//
//        Returns: void
//
//      Reference: none
//
//--------------------------------------------------------------------------------------------------

void print_hex_dump(uint8 * from_here_p, uint16 byte_count)
{
    char  temp_buffer[100] = { 0 };
    char  hex_convert[8]   = { 0 };
    char  ascii_output[20] = { 0 };
    uint8 out_count        = 0;

    // Make sure that we were passed a valid pointer

    if ((uint8 *)NULL == from_here_p)
    {
        // No, that is an invalid pointer

        return;
    }

    // Go until all bytes are done

    while (byte_count-- > 0)
    {
        // Convert the byte to ASCII hex

        (void)sprintf(hex_convert, "%02x ", *from_here_p);

        // Append that ASCII hex to the building string

        (void)strcat(temp_buffer, hex_convert);

        // Build a displayable ASCII version

        if (*from_here_p >= ' ' && *from_here_p <= '~')
        {
            ascii_output[out_count] = (char)*from_here_p;
        }
        else
        {
            ascii_output[out_count] = '.';
        }

        out_count++;
        from_here_p++;

        // Did we display 16 bytes yet?

        if (out_count > 15)
        {
            // Print the line of hex dump

            (void)printf("%s%s\n", temp_buffer, ascii_output);

            // And get ready for the next one

            temp_buffer[0] = 0;
            out_count      = 0;
        }
    }                  

    // Do we have a last line that needs to be printed?

    if (out_count > 0)
    {
        while(out_count++ <= 15)
        {
            (void)strcat(temp_buffer, "   ");

            ascii_output[out_count] = ' ';
        }

        // And send the last line out

        (void)printf("%s%s\n", temp_buffer, ascii_output);
    }
}

//--------------------------------------------------------------------------------------------------
//  Function Name: trace_dump()
//
//    Description: The data passed to this function gets traced in to the
//                 opened file that gets passed to this function.
//
// Pre-Conditions:
//
//     Parameters
//          Local: this_file_p -- A pointer to the opened file to spool
//                 the trace information out
//
//                 this_data_p -- A pointer to the data to trace out
//
//                 this_length -- The number of bytes to trace
//
//                 process_name_p -- The name of the process that requested
//                 the trace spool
//
//                 direction_prompt_p -- The direction of the data (usually
//                 in or out but could be anything.)
//
//                 this_label -- Optional label.  If 0, then no label
//
//        Globals:
//
//        Returns: void
//
//      Reference: none
//
//--------------------------------------------------------------------------------------------------

void trace_dump(FILE * this_file_p,
    uint8 * this_data_p,
    int this_length,
    uint8 * process_name_p,
    uint8 * direction_prompt_p,
    uint16 this_label)
{
    char        temp_buffer[100] = { 0 };
    char        hex_convert[8]   = { 0 };
    char        ascii_output[20] = { 0 };
    uint8       out_count        = 0;
    time_t      the_time         = 0;
    struct tm * local_time       = (struct tm *)NULL;

    // Validate the pointers passed to this function

    if ((FILE *)NULL == this_file_p ||
        (uint8 *)NULL == this_data_p ||
        (uint8 *)NULL == process_name_p ||
        (uint8 *)NULL == direction_prompt_p)
    {
        // Something isn't right

        return;
    }

    // Find out what the current date and time is

    the_time = time((time_t *)NULL);

    // Break that out

    local_time = localtime(&the_time);

    // Format a string

    (void)sprintf(temp_buffer, "%02u%s %02u:%02u:%02u - %s, %s (%x)\n",
        local_time->tm_mday,
        the_months[local_time->tm_mon],
        local_time->tm_hour,
        local_time->tm_min,
        local_time->tm_sec,
        process_name_p,
        direction_prompt_p,
        this_label);

    // Send that to the spool file

    (void)fwrite(temp_buffer, 1, strlen(temp_buffer), this_file_p);

    // Discard what's in the temporary buffer

    temp_buffer[0] = 0;

    // Go until all bytes are done

    while (this_length-- > 0)
    {
        // Convert the byte to ASCII hex

        (void)sprintf(hex_convert, "%02x ", *this_data_p);

        // Append that ASCII hex to the building string

        (void)strcat(temp_buffer, hex_convert);

        // Build a displayable ASCII version

        if (*this_data_p >= ' ' && *this_data_p <= '~')
        {
            ascii_output[out_count] = (char)*this_data_p;
        }
        else
        {
            ascii_output[out_count] = '.';
        }

        out_count++;
        this_data_p++;

        // Did we display 16 bytes yet?

        if (out_count > 15)
        {
            // Send that to the spool file

            (void)fwrite(temp_buffer, 1, strlen(temp_buffer), this_file_p);

            (void)fwrite(ascii_output, 1, strlen(ascii_output), this_file_p);

            (void)fwrite("\n", 1, strlen("\n"), this_file_p);

            // And get ready for the next one

            temp_buffer[0] = 0;
            out_count      = 0;
        }
    }                  

    // Do we have a last line that needs to be sprinted?

    if (out_count > 0)
    {
        while(out_count++ <= 15)
        {
            (void)strcat(temp_buffer, "   ");

            ascii_output[out_count] = ' ';
        }

        // Send that to the spool file

        (void)fwrite(temp_buffer, 1, strlen(temp_buffer), this_file_p);

        (void)fwrite(ascii_output, 1, strlen(ascii_output), this_file_p);

        (void)fwrite("\n", 1, strlen("\n"), this_file_p);
    }
}

int __UNIT_TEST_MAIN (){
	char buf[]="123456890790";
	char file[]="/var/test.log";
	FILE *file_p;
	file_p = fopen(file , "a+");
	if( NULL == file_p ){
		printf("open failed\n");
		return -1;
	}
	print_hex_dump(buf, sizeof(buf) );
	trace_dump( file_p , buf , sizeof(buf) , "test1" , "test2" , 3);
	fflush(file_p);
	fclose(file_p);
	return 0;
}
